#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/11
# 作用就是解耦
import threading, queue

import time


def consume(n):
    while True:
        print("consumer [%s] get task: %s" % (n, q.get()))
        time.sleep(1)
        q.task_done()  # 服务员告诉厨师  包子吃完了 类似广播 只能通知全部线程 无法单独通知


def producer(n):
    count = 1
    while True:
        print("Producer [%s] produced a new task:%s" % (n, count))
        q.put(count)
        count += 1
        q.join()  # 等待开始生产的通知
        print("all tasks has been consumed by consumers...")


q = queue.Queue()
c1 = threading.Thread(target=consume, args=[1, ])
c2 = threading.Thread(target=consume, args=[2, ])
c3 = threading.Thread(target=consume, args=[3, ])

p = threading.Thread(target=producer, args=["xiaoyu", ])
p2 = threading.Thread(target=producer, args=["xiaoyu2", ])

if __name__ == '__main__':
    c1.start()
    c2.start()
    c3.start()
    p.start()
    p2.start()
